package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zcx.helper.secret.SecretAESDESede;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.utils.TimeCount;
import longcaisuyun.longcai.com.utils.Tools;


//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//

/************************************************************/

public class JiaMengActivity extends Activity implements View.OnClickListener {

    Button bt_next;
    EditText et_phone, et_num;
    Button bt_get;
    String code = "";
    TimeCount timeCount;
    private String phone="";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jia_meng);
        View view = getWindow().getDecorView();
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        MyApplication.getInstance().addActivity(this);
        ViewInit();
    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bt_next = (Button) findViewById(R.id.bt_next);
        bt_next.setOnClickListener(this);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_num = (EditText) findViewById(R.id.et_num);
        bt_get = (Button) findViewById(R.id.bt_get);
        bt_get.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_next:
                if (code.length() > 0) {
                    if (!phone.equals(et_phone.getText().toString())){
                        Toast.makeText(JiaMengActivity.this, "您已更换手机号，请重新验证", Toast.LENGTH_SHORT).show();
                    }else if (et_num.getText().toString().equals(code)) {
                        Intent intent = new Intent(JiaMengActivity.this, JiaMengziliaoActivity.class);
                        intent.putExtra("mobile", et_phone.getText().toString());
                        startActivity(intent);
                    } else {
                        Toast.makeText(JiaMengActivity.this, "输入的验证码有误，请检查后重新输入", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(JiaMengActivity.this, "请先进行手机号码验证再进行下一步操作", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.bt_get:
                if (et_phone.getText().toString().length() > 0) {
                    if (Tools.isMobile(et_phone.getText().toString())) {
                        bt_get.setClickable(false);
                        httpPost(et_phone.getText().toString(), "1");
                    } else {
                        Toast.makeText(JiaMengActivity.this, "输入的手机号码格式错误，请检查后重新输入", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(JiaMengActivity.this, "您还未输入手机号码", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void httpPost(final String mobile, String action) {
        final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
        final FinalHttp finalHttp = new FinalHttp();
        AjaxParams params = new AjaxParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", mobile);
            jsonObject.put("action", action);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        finalHttp.post(publicUrl.mobilecode, params, new AjaxCallBack<String>() {
            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(JiaMengActivity.this, finalHttp, publicUrl.mobilecode);
            }

            public void onSuccess(String s) {
                super.onSuccess(s);
                try {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(secretAESDESede.decrypt(s));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String Message = jsonObject.getString("message");
                    if (Message.equals("1")) {
                        timeCount = new TimeCount(60000, 1000, bt_get);
                        timeCount.start();
                        code = jsonObject.getString("code");
                        Log.e("code",code);
                        phone=mobile;
                        Toast.makeText(JiaMengActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                    }
                    if (Message.equals("2")) {
                        bt_get.setClickable(true);
                        Toast.makeText(JiaMengActivity.this, "该手机号码已经注册，请更换另一手机号码进行注册", Toast.LENGTH_SHORT).show();
                    }
                    if (Message.equals("0")) {
                        bt_get.setClickable(true);
                        Toast.makeText(JiaMengActivity.this, "获取验证码失败，请重新获取", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
                bt_get.setClickable(true);
                Toast.makeText(JiaMengActivity.this, "获取验证码失败，请重新获取", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
